<template>
  <div class="course-create">
    <el-card>
      <div slot="header">
        <el-steps :active="activeStep" simple>
          <el-step
            v-for="(step, i) in steps"
            :key="step.id"
            :title="step.title"
            :icon="step.icon"
            @click.native="activeStep = i"
          ></el-step>
        </el-steps>
      </div>

      <el-form label-width="80px" :model="course">
        <div v-show="activeStep === 0">
          <el-form-item label="课程名称" prop="courseName">
            <el-input v-model="course.courseName"></el-input>
          </el-form-item>
          <el-form-item label="课程简介" prop="brief">
            <el-input v-model="course.brief"></el-input>
          </el-form-item>
          <div style="display: flex;">
            <el-form-item label="课程概述" prop="previewFirstField" style="flex: 1;">
              <el-input v-model="course.previewFirstField"></el-input>
            </el-form-item>
            <el-form-item prop="previewSecondField" style="flex: 1;">
              <el-input v-model="course.previewSecondField"></el-input>
            </el-form-item>
          </div>
          <el-form-item label="讲师姓名" prop="teacherDTO.teacherName">
            <el-input v-model="course.teacherDTO.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="讲师简介" prop="teacherDTO.description">
            <el-input v-model="course.teacherDTO.description"></el-input>
          </el-form-item>
          <el-form-item label="课程排序" prop="sortNum">
            <el-input-number
              label="描述文字"
              v-model="course.sortNum"
            ></el-input-number>
          </el-form-item>

          <el-form-item>
            <el-button @click="activeStep += 1;">下一步</el-button>
          </el-form-item>
        </div>

        <div v-show="activeStep === 1">
          <course-image v-model="course.courseListImg" label="课程封面"></course-image>
          <course-image v-model="course.courseImgUrl" label="解锁封面"></course-image>
          <el-form-item>
            <el-button @click="activeStep += 1;">下一步</el-button>
          </el-form-item>
        </div>

        <div v-show="activeStep === 2">
          <el-form-item label="售卖价格">
            <el-input v-model="course.discounts" type="number" :min="0">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="商品原价">
            <el-input v-model="course.price" type="number" :min="0">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="销量">
            <el-input v-model="course.sales" type="number" :min="0">
              <template slot="append">单</template>
            </el-input>
          </el-form-item>
          <el-form-item label="活动标签">
            <el-input v-model="course.discountsTag"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="activeStep += 1;">下一步</el-button>
          </el-form-item>
        </div>

        <div v-show="activeStep === 3">
          <el-form-item label="限时秒杀开关" label-width="100px">
            <el-switch v-model="course.activityCourse" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          </el-form-item>
          <template v-if="course.activityCourse">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="course.activityCourseDTO.beginTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="course.activityCourseDTO.endTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="秒杀价">
              <el-input
                v-model="course.activityCourseDTO.amount"
                type="number"
                :min="0"
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="库存">
              <el-input
                v-model="course.activityCourseDTO.stock"
                type="number"
                :min="0"
              >
                <template slot="append">个</template>
              </el-input>
            </el-form-item>
          </template>
          <el-form-item>
            <el-button @click="activeStep += 1;">下一步</el-button>
          </el-form-item>
        </div>

        <div v-show="activeStep === 4">
          <el-form-item label="课程详情">
            <text-editor v-model="course.courseDescriptionMarkDown"></text-editor>
          </el-form-item>
          <el-form-item label="是否上架">
            <el-switch
              v-model="course.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleSave">保存</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import CourseImage from './CourseImage'
import TextEditor from '@/components/TextEditor'
import { saveOrUpdateCourse, getCourseById } from '@/services/course'
export default {
  components: {
    CourseImage,
    TextEditor
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [Number, String]
    }
  },
  data () {
    return {
      activeStep: 0,
      steps: [
        { id: 1, title: '基本信息', icon: 'el-icon-edit' },
        { id: 2, title: '课程封面', icon: 'el-icon-upload' },
        { id: 3, title: '销售信息', icon: 'el-icon-picture' },
        { id: 4, title: '秒杀信息', icon: 'el-icon-picture' },
        { id: 5, title: '课程详情', icon: 'el-icon-picture' }
      ],
      course: {
        courseName: '',
        brief: '',
        previewFirstField: '',
        previewSecondField: '',
        teacherDTO: {
          teacherName: '',
          description: ''
        },
        sortNum: 0,
        discounts: 0,
        price: 0,
        sales: 0,
        discountsTag: '',
        activityCourseDTO: {
          beginTime: '',
          endTime: '',
          amount: 0,
          stock: 0
        }
      }
    }
  },
  created () {
    if (this.isEdit) {
      this.loadCourse()
    }
  },
  methods: {
    async handleSave () {
      const { data } = await saveOrUpdateCourse(this.course)
      if (data.code === '000000') {
        this.$message.success(`${this.isEdit ? '编辑' : '新建'}课程成功`)
        this.$router.push({
          name: 'course'
        })
      }
    },
    async loadCourse () {
      const { data } = await getCourseById(this.courseId)
      if (data.code === '000000') {
        if (data.data.activityCourse) {
          data.data.activityCourseDTO = {
            beginTime: '',
            endTime: '',
            amount: 0,
            stock: 0
          }
        }
        this.course = data.data
      }
    }
  }
}
</script>

<style>
.el-step {
  cursor: pointer;
}
</style>
